/*
 * @Author: 唉诺
 * @Date: 2021-03-07 20:10:56
 */
export default class Enemy {
  constructor (game, config) {
    this.game = game
    this.config = config
    this.enemysGroup = this.game.add.group()
    this.enemyBulletGroup = this.game.add.group()
  }
  init () {
    this.enemysGroup.enableBody = true
    this.enemysGroup.createMultiple(10, 'enemy')
    this.enemysGroup.setAll('outOfBoundsKill', true)
    this.enemysGroup.setAll('checkWorldBounds', true)
    this.enemysGroup.setAll('width', dpi(64))
    this.enemysGroup.setAll('height', dpi(64))
    this.enemysGroup.setAll('resolution', 1)
    // 敌人子弹
    this.enemyBulletGroup.enableBody = true
    this.enemyBulletGroup.createMultiple(50, 'bullet')
    this.enemyBulletGroup.setAll('outOfBoundsKill', true)
    this.enemyBulletGroup.setAll('checkWorldBounds', true)
    this.enemyBulletGroup.setAll('width', dpi(8))
    this.enemyBulletGroup.setAll('height', dpi(15))
    this.enemyBulletGroup.setAll('top', 0)
    this.enemyBulletGroup.setAll('left', 0)

    // 敌人的爆炸效果
    // this.boomGroup = this.add.group()
    // this.boomGroup.createMultiple(10, 'boom')

    // this.boomGroup.forEach((boom) => {
    //   boom.animations.add('boom')
    // }, this)
    // 敌人的随机位置范围
    this.maxWidth = this.game.width - dpi(64)
  }
  generateEnemy () {
    const enemy = this.enemysGroup.getFirstExists(false)
    if (enemy) {
      enemy.isHit = false
      enemy.loadTexture('enemy', 0)
      enemy.reset(this.game.rnd.integerInRange(0, this.maxWidth), 10)
      // = config.life
      enemy.body.velocity.y = dpi(100)
      this.enemyOpenFire(enemy)
    }
  }
  // 敌人我开火
  enemyOpenFire (enemy) {
    // this.enemysGroup.forEach((enemy) => {
    const bullet = this.enemyBulletGroup.getFirstExists(false)
    if (bullet) {
      bullet.reset(enemy.x + dpi(64) / 2 + dpi(4), enemy.y + dpi(64))
      bullet.angle = -180

      bullet.body.velocity.y = dpi(200)
    }
    // })
  }
}
